<?php

// 瞎猜接口类库
// 2017-9-30

class xiacaiModule extends baseModule {

    /**
     * 瞎猜栏目首页列表界面(需登录)
     * 
     * @2017-9-30
     */
    public function xiacai_video() {
        $root = array();
        if (!$GLOBALS['user_info']) {
            $root['error'] = "用户未登陆,请先登陆.";
            $root['status'] = 0;
            $root['user_login_status'] = 0; //有这个参数： user_login_status = 0 时，表示服务端未登陆、要求登陆，操作
        } else {
            $root['status'] = 1;
            $root['has_next'] = false;
            $root['page'] = 1;
        }
        $page = intval($_REQUEST['page']); // 当前页面
        $pagesize = intval($_REQUEST['pagesize']);
        if ($page < 1) {
            $page = 1;
        }
        if ($pagesize < 1) {
            $pagesize = 10;
        }
        $root['page'] = $page;
        $user_id = intval($GLOBALS['user_info']['id']);
        // @2017 新增用户钻石和金币的读取        
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
        $user_redis = new UserRedisService();
        if (OPEN_DIAMOND_GAME_MODULE == 1) {
            $user_data = $user_redis->getRow_db($user_id, array('id', 'head_image', 'diamonds'));
            //游戏币
            $coins = 0;
        } else {
            $user_data = $user_redis->getRow_db($user_id, array('id', 'head_image', 'diamonds', 'coin'));
            //游戏币
            $coins = $user_data['coin'];
        }
        // 用户信息(钻石,金币)
        $root['user_info'] = array('user_id' => $user_id, 'diamonds' => $user_data['diamonds'], 'coins' => $coins, 'head_image' => get_spec_image($user_data['head_image']));

        // 是否已订阅
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/BaseRedisService.php');
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserSubscribeRedisService.php');
        $subscribe_redis = new UserSubscribeRedisService($user_id);
        // 是否正在直播中
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoRedisService.php');
        $video_redis = new VideoRedisService();

        // 有分页只能用数据库查询,无法用reids优化
        // 正在直播的节目列表 (有room_id并按照room_id最新排序)
        $limit = ($pagesize * ($page - 1)) . ',' . ($pagesize * $page);
        $sqlstmt = 'select xc.id, xc.image_url, xc.start_time_tip, xc.room_id, v.user_id, v.group_id, v.live_image, v.create_type, v.live_in from ' . DB_PREFIX . 'xiacai xc inner join ' . DB_PREFIX . 'video v on xc.room_id=v.id order by xc.room_id desc limit ' . $limit;
        $cache_time = 0;
        $cacheid = 'xiacai_liveing_' . md5($sqlstmt);
        $list = $GLOBALS['cache']->get($cacheid, true);
        $skipids = array();
        if (false === $list || !$cache_time) {
            $list = $GLOBALS['db']->getAll($sqlstmt);
            $newlist = array();
            foreach ($list as $row) {
                $row['is_subscribe'] = $subscribe_redis->is_subscribe($row['id']) ? 1 : 0;
                $row['is_living'] = $row['room_id'] > 0 ? 1 : 0;
                $newlist[] = $row;
                $skipids[] = $row['id'];
            }
            $root['living_video'] = $newlist;
            if ($cache_time) {
                $GLOBALS['cache']->set($cacheid, $newlist, $cache_time, true);
            }
        } else {
            $root['living_video'] = $list;
            foreach ($list as $row) {
                $skipids[] = $row['id'];
            }
        }
        /*
          fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/XiacaiRedisService.php');
          $xiacai_redis = new XiacaiRedisService();
          $living_video = $xiacai_redis->get_m_xiacai(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
          $living_video2 = array();
          foreach ($living_video as $xiacai_id => $row) {
          if (!$xiacai_id) {
          continue;
          }
          $row['id'] = $xiacai_id;
          $row['is_subscribe'] = $subscribe_redis->is_subscribe($xiacai_id) ? 1 : 0;
          //$row['is_living'] = $video_redis->getOne_db($row['id'], 'live_in');
          $row['is_living'] = 1;
          $living_video2[] = array('id' => $xiacai_id, 'image_url' => $row['image_url'], 'is_subscribe' => $row['is_subscribe'], 'is_living' => $row['is_living'], 'start_time_tip' => $row['start_time_tip']);
          }
          $root['living_video'] = $newlist; */
        // 已订阅未直播的节目列表 
        $skipsql = empty($skipids) ? '' : ' and xc.id not in (' . implode(',', $skipids) . ')';
        $sqlstmt = "select xc.id, xc.image_url, xc.start_time_tip, xc.room_id from " . DB_PREFIX . "xiacai xc inner join " . DB_PREFIX . "xiacai_subscribe sube on xc.id=sube.xiacai_id where sube.user_id=$user_id and xc.room_id=0 $skipsql order by sube.id desc limit $limit";
        $cacheid = 'xiacai_subscribe_noliving_' . $user_id . '_' . md5($sqlstmt);
        $list = $GLOBALS['cache']->get($cacheid, true);
        if (false === $list || !$cache_time) {
            $list = $GLOBALS['db']->getAll($sqlstmt);
            $newlist = array();
            foreach ($list as $row) {
                $row['is_subscribe'] = 1;
                $row['is_living'] = 0;
                $newlist[] = $row;
                $skipids[] = $row['id'];
            }
            $root['xiacai_subscribe_video'] = $newlist;
            if ($cache_time) {
                $GLOBALS['cache']->set($cacheid, $newlist, $cache_time, true);
            }
        } else {
            $root['xiacai_subscribe_video'] = $list;
            foreach ($list as $row) {
                $skipids[] = $row['id'];
            }
        }
        /*
          fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserSubscribeRedisService.php');
          $usersubscribe_redis = new UserSubscribeRedisService($user_id);
          $subscribe_list = $usersubscribe_redis->get_subscribe_list();
          $root['xiacai_subscribe_video'] = $living_video2; */
        // 未订阅未直播的节目列表
        $skipsql = empty($skipids) ? '' : ' and id not in (' . implode(',', $skipids) . ')';
        $sqlstmt = "select id, image_url, start_time_tip, room_id from " . DB_PREFIX . "xiacai where room_id=0 and id not in (select id from " . DB_PREFIX . "xiacai_subscribe where user_id=$user_id) $skipsql order by id desc limit $limit";
        $cacheid = 'xiacai_unsubscribe_' . $user_id . '_' . md5($sqlstmt);
        $list = $GLOBALS['cache']->get($cacheid, true);
        if (false === $list || !$cache_time) {
            $list = $GLOBALS['db']->getAll($sqlstmt);
            $newlist = array();
            foreach ($list as $row) {
                $row['is_subscribe'] = 0;
                $row['is_living'] = $row['room_id'] > 0 ? 1 : 0;
                $newlist[] = $row;
            }
            $root['xiacai_unsubscribe_video'] = $newlist;
            if ($cache_time) {
                $GLOBALS['cache']->set($cacheid, $newlist, $cache_time, true);
            }
        } else {
            $root['xiacai_unsubscribe_video'] = $list;
        }
        /*
          $unsubscribe_list = $usersubscribe_redis->get_unsubscribe_list();
          $root['xiacai_unsubscribe_video'] = is_array($unsubscribe_list) ? $unsubscribe_list : $living_video2; */
        debugmsg('xiacai.action.php::xiacai_video()', $root);
        ajax_return($root);
    }

    /**
     * 瞎猜节目详情界面接口(需登录)
     */
    public function detail() {
        $root = array();
        if (!$GLOBALS['user_info']) {
            $root['error'] = "用户未登陆,请先登陆.";
            $root['status'] = 0;
            $root['user_login_status'] = 0; //有这个参数： user_login_status = 0 时，表示服务端未登陆、要求登陆，操作
        } else {
            $root['status'] = 1;
            $root['has_next'] = false;
            $root['page'] = 1;
        }
        $page = intval($_REQUEST['page']); // 当前页面
        $pagesize = intval($_REQUEST['pagesize']);
        if ($page < 1) {
            $page = 1;
        }
        if ($pagesize < 1) {
            $pagesize = 10;
        }
        $root['page'] = $page;
        $user_id = intval($GLOBALS['user_info']['id']);
        $xiacai_id = intval($_REQUEST['xiacai_id']); // 瞎猜ID
        // 详情信息
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/XiacaiRedisService.php');
        $xiacai_redis = new XiacaiRedisService();
        $detail = $xiacai_redis->getRow_db($xiacai_id);

        // 是否已订阅
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserSubscribeRedisService.php');
        $subscribe_redis = new UserSubscribeRedisService($user_id);
        $detail['is_subscribe'] = $subscribe_redis->is_subscribe($xiacai_id) ? 1 : 0;

        // 是否正在直播中
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoRedisService.php');
        $video_redis = new VideoRedisService();
        $video_info = $video_redis->getRow_db($detail['room_id']);
        if ($video_info) {
            $video_fields = array('group_id', 'live_image', 'create_type', 'live_in');
            foreach ($video_fields as $video_field) {
                $detail[$video_field] = $video_info[$video_field];
            }
        }
        //$detail['is_living'] = $video_redis->getOne_db($detail['room_id'], 'live_in');
        $detail['is_living'] = $detail['room_id'] > 0 ? 1 : 0;

        $root['detail'] = $detail;

        // 回播列表
        $limit = ($pagesize * ($page - 1)) . ',' . ($pagesize * $page);
        $sqlstmt = 'select v.`id`, v.`user_id`, v.`title`, v.group_id, v.live_image, v.create_type, v.live_in, v.`begin_time`, v.`end_time`, v.`push_rtmp`, v.`play_flv`, v.`play_rtmp`, v.`play_mp4`, v.`play_hls`, v.`xpoint`, v.`ypoint`, v.xiacai_id, u.nick_name, xc.image_url from ' . DB_PREFIX . 'video_history v inner join ' . DB_PREFIX . 'user u on v.user_id=u.id inner join ' . DB_PREFIX . 'xiacai xc on v.xiacai_id=xc.id where v.xiacai_id=' . $xiacai_id . ' and v.is_del_vod=0 order by v.id desc limit ' . $limit;
        $cacheid = 'xiacai_video_history_' . $xiacai_id . '_' . md5($sqlstmt);
        $list = $GLOBALS['cache']->get($cacheid, true);
        if (false === $list) {
            $list = $GLOBALS['db']->getAll($sqlstmt);
            $GLOBALS['cache']->set($cacheid, $list, 3, true);
        }
        $root['video_history'] = $list;
        ajax_return($root);
    }

}
